package teste.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import teste.Cliente;
import teste.HibernateUtil;

public class ClienteDAO {

	private Session session;
	
	public ClienteDAO() {
	}
	
	public void salvar(Cliente cli) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(cli);
		session.getTransaction().commit();
	}
	
	public List listar(){
		session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List l1 = session.createQuery("from Cliente").list();
		session.getTransaction().commit();
		return l1;
	}
	
	public List PesquisarPorNome(String nomeConsultar) {
		session = HibernateUtil.getSessionFactory().openSession();
		Query q = session.getNamedQuery("cliente.igual");
		q.setString("nome", nomeConsultar);
		session.beginTransaction();
		List resultado = q.list();
		session.getTransaction().commit();
		session.close();
		return resultado;
	}
	
	public List PesquisarAproximacao(String nomeConsultar) {
		session = HibernateUtil.getSessionFactory().openSession();
		Query q = session.getNamedQuery("cliente.pesquisa");
		q.setString("nome", "%"+nomeConsultar+"%");
		session.beginTransaction();
		List resultado = q.list();
		session.getTransaction().commit();
		session.close();
		return resultado;
	}

}
